home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 7661 / 7661.xpi / components / RILAPIRequest.js next >
Text File  |  2009-12-23  |  7KB  |  191 lines

  1. /*
  2.  
  3. License: This source code may not be used in other applications whether they
  4. be personal, commercial, free, or paid without written permission from Read It Later.
  5.  
  6.  
  7. /////////
  8. DEVELOPER API: readitlaterlist.com/api/
  9. /////////
  10.  
  11. If you would like to customize Read It Later or build an application that works with
  12. Read it Later take a look at the READ IT LATER OPEN API:
  13. http://readitlaterlist.com/api/
  14.  
  15. Suggestions for additions to Read It Later are VERY welcome.  A large number of user
  16. suggestions have been implemented.  Please let me know of any additional features you
  17. are seeking at: http://readitlaterlist.com/support/
  18.  
  19. Thanks
  20.  
  21. */
  22.  
  23. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  24.  
  25. function RILAPIrequest() {    
  26.     this.keyed = true;
  27.     
  28.     this.APP    = Components.classes['@ril.ideashower.com/rildelegate;1'].getService().wrappedJSObject;
  29.     this.IO = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  30.     this.OBS = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  31.     this.STREAM = Components.classes["@mozilla.org/scriptableinputstream;1"];
  32.     
  33.     // If you need an API key, get one for free at http://readitlaterlist.com/api/signup/
  34.     // Do not use this one, it does not have any special privileges and it is still rate-limited.
  35.     // I use it to maintain usage stats, so I'd appreciate it if you didn't muddle it up :-)
  36.     this.apikey     = 'c4cT4A38g0G92Z820ldU509d6fp5g49b';
  37.     this.api        = 'https://readitlaterlist.com/v2/';
  38.     this.apiText    = 'https://text.readitlaterlist.com/v2/';
  39. }
  40.  
  41. // class definition
  42. RILAPIrequest.prototype = {
  43.  
  44.   // properties required for XPCOM registration:
  45.   classDescription: "Read It Later API Request Javascript XPCOM Component",
  46.   classID:          Components.ID("{8154b020-aabf-11de-8a39-0800200c9a66}"),
  47.   contractID:       "@ril.ideashower.com/rilapirequest;1",
  48.  
  49.   QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIRILAPIRequest]),
  50.  
  51.   //////////////////////////////////////////    
  52.     
  53.     initAndStart : function(method, login, params, errorReporting, methodDescription)
  54.     {
  55.         this.init(method, login, params, errorReporting, methodDescription);
  56.         this.start();
  57.         return this.requestId;
  58.     },
  59.     
  60.     init : function(method, login, params, errorReporting, methodDescription)
  61.     {        
  62.         this.method = method;
  63.         this.login = login;
  64.         this.params = params;
  65.     this.methodDescription = methodDescription;
  66.         this.errorReporting = errorReporting ? errorReporting : 'all';
  67.         
  68.         // Make request id
  69.         this.requestId = this.APP.now() + this.method + this.params + Math.random();
  70.         
  71.         return this.requestId;
  72.     },
  73.     
  74.     // -- //
  75.     
  76.     start : function ()
  77.     {
  78.     try {
  79.             let url;
  80.             
  81.             if (this.method == 'text')
  82.                 url = this.apiText + 'firefox';
  83.             else
  84.                 url = this.api + this.method;
  85.         
  86.         // Params
  87.         let params = this.params;
  88.         if (this.keyed) params += '&apikey='+this.apikey;
  89.         if (this.login)
  90.         {
  91.         let currentLogin = this.APP.getLogin();
  92.         if (currentLogin)
  93.             params += '&username='+this.APP.e(currentLogin.username)+'&password='+this.APP.e(currentLogin.password);
  94.         }
  95.         
  96.         this.APP.d('--');
  97.         this.APP.d(url);
  98.         this.APP.d(params);
  99.         this.APP.d('--');
  100.         
  101.         this.transport = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);  
  102.         this.osc(this);
  103.         this.transport.open("POST", url, true);
  104.         this.transport.setRequestHeader("User-Agent" , 'Read It Later Firefox ' + this.APP.v);
  105.         this.transport.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
  106.         this.transport.send(params);
  107.         return;
  108.     } catch(e){Components.utils.reportError(e);}
  109.     
  110.     // else
  111.     this.transport = {status:0};
  112.     this.finished();
  113.     },
  114.         
  115.     osc : function (ajaxObj)
  116.     {
  117.     ajaxObj.transport.onreadystatechange = function() {
  118.         try {
  119.         switch(ajaxObj.transport.readyState) {
  120.             case(4):
  121.             ajaxObj.finished();
  122.         }
  123.         }
  124.         catch( e ) { //server error
  125.             //alert('server error');
  126.         }
  127.     }    
  128.     },
  129.     
  130.     // -- //
  131.     
  132.     finished : function()
  133.     {
  134.     try {
  135.             this.success = (this.transport.status == '200');
  136.             this.response = this.transport.responseText;
  137.         this.status = this.transport.status;
  138.         
  139.             if (!this.success) {
  140.             
  141.                 this.error = this.header('X-Error') ? this.header('X-Error') : 'Could not reach Read It Later.  Make sure you are connected to the internet.';
  142.                 
  143.                 // Error
  144.                 if (this.errorReporting != 'none')
  145.                 {
  146.                     
  147.                     if (this.transport.status == '401')
  148.                     {
  149.                         this.APP.genericMessage('Your username and password are not correct.\nIf you recently changed your username and/or password, you will need to relogin.',
  150.                                            [
  151.                                             {label:'Relogin', delegate:this.APP.getTopRIL(), selector:'relogin'},
  152.                                             {label:'Get Help', delegate:this.APP.getTopRIL(), selector:'getHelp'}
  153.                                            ], false, 'Sync', true);
  154.                         
  155.                     }
  156.                     else
  157.                     {
  158.                         let action = this.methodDescription == 'read' ? 'getting your archive' : 'syncing';
  159.                         this.APP.genericMessage('There was a problem while ' + action + ':\n'+this.error,
  160.                                            [
  161.                                             {label:'Try Again', delegate:this.methodDescription == 'read' ? this.APP.getTopRIL() : this.APP.SYNC, selector: this.methodDescription == 'read' ? 'updateReadList' : 'sync'},
  162.                                             {label:'Get Help', delegate:this.APP.getTopRIL(), selector:'getHelp'}
  163.                                            ], false, 'Sync', true);
  164.                     }
  165.                     
  166.                 }
  167.                 
  168.             }
  169.             
  170.             this.OBS.notifyObservers(this, 'ril-api-request-finished', this.requestId);
  171.     } catch(e) {Components.utils.reportError(e);}
  172.     },
  173.     
  174.     setHeader : function(name, value)
  175.     {
  176.     return this.transport.getResponseHeader(name);    
  177.     },
  178.     
  179.     header : function(name)
  180.     {
  181.     if (this.transport)
  182.         return this.transport.getResponseHeader(name);    
  183.     }
  184.     
  185. };
  186.  
  187. var components = [RILAPIrequest];
  188. function NSGetModule(compMgr, fileSpec) {
  189.   return XPCOMUtils.generateModule(components);
  190. }
  191.